package com.tsingtree.modular.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsingtree.base.pojo.page.LayuiPageFactory;
import com.tsingtree.base.pojo.page.LayuiPageInfo;
import com.tsingtree.modular.entity.NanSellAnnounce;
import com.tsingtree.modular.entity.NanSellAnnounceComment;
import com.tsingtree.modular.entity.NanSellAnnounceReply;
import com.tsingtree.modular.mapper.NanSellAnnounceCommentMapper;
import com.tsingtree.modular.model.params.NanSellAnnounceCommentParam;
import com.tsingtree.modular.model.result.NanSellAnnounceCommentResult;
import com.tsingtree.modular.service.NanSellAnnounceCommentService;
import com.tsingtree.modular.service.NanSellAnnounceReplyService;
import com.tsingtree.modular.service.NanSellAnnounceService;
import com.tsingtree.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author jagger
 * @since 2024-03-08
 */
@Service
public class NanSellAnnounceCommentServiceImpl extends ServiceImpl<NanSellAnnounceCommentMapper, NanSellAnnounceComment> implements NanSellAnnounceCommentService {

    @Autowired
    private NanSellAnnounceService nanSellAnnounceService;

    @Autowired
    private NanSellAnnounceReplyService nanSellAnnounceReplyService;

    @Override
    public void add(NanSellAnnounceCommentParam param){
        NanSellAnnounceComment entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(NanSellAnnounceCommentParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(NanSellAnnounceCommentParam param){
        NanSellAnnounceComment oldEntity = getOldEntity(param);
        NanSellAnnounceComment newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public NanSellAnnounceCommentResult findBySpec(NanSellAnnounceCommentParam param){
        return null;
    }

    @Override
    public List<NanSellAnnounceCommentResult> findListBySpec(NanSellAnnounceCommentParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(NanSellAnnounceCommentParam param){
        Page pageContext = getPageContext();
        IPage<NanSellAnnounceCommentResult> page = this.baseMapper.customPageList(pageContext, param);
        Map<Integer, Integer> map = new HashMap<>();
        if(!CollectionUtils.isEmpty(page.getRecords())){
            List<Integer> commentIds = page.getRecords().stream().map(NanSellAnnounceCommentResult::getId).collect(Collectors.toList());
            List<Map<String, Object>> objects = nanSellAnnounceReplyService.listMaps(new QueryWrapper<NanSellAnnounceReply>()
                    .select("count(1) as count, comment_id")
                            .eq("is_del", false)
                    .in("comment_id", commentIds).groupBy("comment_id"));
            objects.forEach((m) -> {
                map.put(Integer.parseInt(m.get("commentId").toString()), Integer.parseInt(m.get("count").toString()));
            });
        }
        page.getRecords().forEach(comment -> {
            comment.setReplyCount(map.getOrDefault(comment.getId(), 0));
        });
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(List<NanSellAnnounceReply> list, NanSellAnnounceComment nanSellAnnounceComment, NanSellAnnounce nanSellAnnounce) {
        if(!CollectionUtils.isEmpty(list)){
            nanSellAnnounceReplyService.updateBatchById(list);
        }
        if(!this.updateById(nanSellAnnounceComment)){
            throw new RuntimeException();
        }
        if(!nanSellAnnounceService.updateById(nanSellAnnounce)){
            throw new RuntimeException();
        }
    }

    private Serializable getKey(NanSellAnnounceCommentParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private NanSellAnnounceComment getOldEntity(NanSellAnnounceCommentParam param) {
        return this.getById(getKey(param));
    }

    private NanSellAnnounceComment getEntity(NanSellAnnounceCommentParam param) {
        NanSellAnnounceComment entity = new NanSellAnnounceComment();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
